package test;

import java.io.*;
import java.net.*;

public class SimpleThread {

    SimpleThread() {
    }

    public static void main(String[] args) {
        String modifiedSentence = null;
        System.out.println("Client now running");
        // create a buffered reader to get the data from the user through the
        // command line //
        // a client socket //
        Socket clientSocket = null;
        try {
            /**
             * construct the client socket, specify the IP address which is here
             * localhost for the loopback device and the port number at which a
             * connection needs to be initialized.
             */
            clientSocket = new Socket("localhost", 7000);
            // These streams are for the same purpose as in the server side //
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream()));
            // read the user's input from the console. //
            String sentence = "<create><pass1><marc><farra><1990-10-01><marcfarra@gmail.com>";
            // send the sentence to the server: note the need to append it with
            // an endline character //
            outToServer.writeBytes(sentence + '\n');
            // listen on the socket and read the reply of the server //
            modifiedSentence = inFromServer.readLine();
        } catch (UnknownHostException ex) {// catch the exceptions //
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println(
                "FROM SERVER: " + modifiedSentence);

        try {
            // close the socket when done //
            clientSocket.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
